import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:intl/intl.dart';
import '../../resources/kq_theme_colors.dart';
import 'package:kq_flutter_core_widget/widgets/image/kq_image.dart';
import 'package:kq_flutter_widgets/widgets/progressbar/kq_horizontal_progress_bar.dart';

import '../../config/kq_global.dart';
import '../../resources/images.dart';

///KqSeekbar分离出来的定制性较高的Seekbar.
///
///1.可设置Seekbar按钮widget.
///
///2.可设置高度，[height]控件高度、[progressHeight]进度条高度
///
/// 3.可格式化数据
class SeekBar extends StatefulWidget {
  /// seekbar的值的回调
  final void Function(double one, double two)? callback;

  /// seekbar滑动结束时的值的回调
  final void Function(double one, double two)? callbackWhenFinish;

  /// seekBar值的format
  final NumberFormat? numberFormat;

  /// 是否是双滑块，默认为否
  final bool isTwoSeekBar;

  /// 第一个seekbar位置
  final double? valueOneSeekBar;

  /// 第二个seekbar位置
  final double? valueTwoSeekBar;

  /// 进度条最大值
  final double max;

  ///背景进度颜色
  final Color bgColor;

  ///只有一个seekbar的进度颜色
  final Color oneSeekColor;

  ///有两个seekbar时第二个进度的颜色
  final Color twoSeekColor;

  ///第一个seekbar Widget
  final Widget? oneSeekWidget;

  ///第二个seekbar Widget
  final Widget? twoSeekWidget;

  ///进度条的高度
  final double? progressHeight;

  ///控件高度，默认44.r
  final double? height;

  const SeekBar({
    super.key,
    this.callback,
    this.callbackWhenFinish,
    this.isTwoSeekBar = false,
    this.valueOneSeekBar,
    this.valueTwoSeekBar,
    this.max = 100,
    this.bgColor = KqThemeColors.bgF5,
    this.oneSeekColor = KqThemeColors.textLightBlue,
    this.twoSeekColor = KqThemeColors.bgF5,
    this.numberFormat,
    this.oneSeekWidget,
    this.twoSeekWidget,
    this.progressHeight,
    this.height,
  });

  @override
  State<StatefulWidget> createState() => _SeekBarState();
}

class _SeekBarState extends State<SeekBar> {
  ///进度条的左右边距
  double _padding = 0;
  double _progress = 0;
  double _secondProgress = 0;
  double _max = 0;

  @override
  void initState() {
    super.initState();
    _max = widget.max;
    _progress = widget.valueOneSeekBar ?? _max / 2;
    _secondProgress = widget.valueTwoSeekBar ?? 0;
    _padding = widget.height != null ? widget.height! / 2 : 22.r;

    _max = _format(widget.numberFormat, _max);
  }

  @override
  void didUpdateWidget(covariant SeekBar oldWidget) {
    super.didUpdateWidget(oldWidget);
    _max = widget.max;
    _progress = widget.valueOneSeekBar ?? _max / 2;
    _secondProgress = widget.valueTwoSeekBar ?? 0;
    _padding = widget.height != null ? widget.height! / 2 : 22.r;

    _max = _format(widget.numberFormat, _max);
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, container) {
        double width = container.maxWidth;
        double progressWidth = width - 2 * _padding;
        double leftOne = progressWidth * _progress / _max;
        double leftTwo = progressWidth * _secondProgress / _max;
        return SizedBox(
          width: double.infinity,
          height: widget.height ?? 44.r,
          child: Stack(
            alignment: Alignment.center,
            children: [
              Positioned(
                  left: _padding,
                  right: _padding,
                  child: KqHorizontalProgressBar(
                    height: widget.progressHeight ?? 4.r,
                    max: _max,
                    bgColor: widget.bgColor,
                    pbColor: widget.oneSeekColor,
                    secondPbColor: widget.twoSeekColor,
                    enableSecondProgress: widget.isTwoSeekBar ? true : false,
                    secondProgressShape: SecondProgressShape.roundRect,
                    progress: _progress,
                    secondProgress: _secondProgress,
                  )),
              Positioned(
                left: leftOne,
                child: GestureDetector(
                  onHorizontalDragUpdate: (e) {
                    _progress += _max * e.delta.dx / progressWidth;
                    if (_progress <= 0) {
                      _progress = 0;
                      if (_progress <= _secondProgress) {
                        _secondProgress = 0;
                      }
                    } else if (_progress >= _max) {
                      _progress = _max;
                    } else if (_progress <= _secondProgress) {
                      _secondProgress = _progress;
                    }
                    widget.callback?.call(
                        _format(widget.numberFormat, _progress),
                        _format(widget.numberFormat, _secondProgress));
                    setState(() {});
                  },
                  onHorizontalDragEnd: (e) {
                    widget.callbackWhenFinish?.call(
                        _format(widget.numberFormat, _progress),
                        _format(widget.numberFormat, _secondProgress));
                  },
                  child: widget.oneSeekWidget ??
                      KqImage.assets(
                        url: Images.commonIcHuakuai28,
                        package: KqGlobal.packageName,
                        width: 44.r,
                        height: 44.r,
                      ),
                ),
              ),
              Positioned(
                left: leftTwo,
                child: Visibility(
                  visible: widget.isTwoSeekBar,
                  child: GestureDetector(
                    onHorizontalDragUpdate: (e) {
                      _secondProgress += _max * e.delta.dx / progressWidth;
                      if (_secondProgress <= 0) {
                        _secondProgress = 0;
                      } else if (_secondProgress >= _max) {
                        _secondProgress = _max;
                        if (_secondProgress >= _progress) {
                          _progress = _max;
                        }
                      } else if (_secondProgress >= _progress) {
                        _progress = _secondProgress;
                      }
                      widget.callback?.call(
                          _format(widget.numberFormat, _progress),
                          _format(widget.numberFormat, _secondProgress));
                      setState(() {});
                    },
                    onHorizontalDragEnd: (e) {
                      widget.callbackWhenFinish?.call(
                          _format(widget.numberFormat, _progress),
                          _format(widget.numberFormat, _secondProgress));
                    },
                    child: widget.twoSeekWidget ??
                        KqImage.assets(
                          url: Images.commonIcHuakuai28,
                          package: KqGlobal.packageName,
                          width: 44.r,
                          height: 44.r,
                        ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  double _format(NumberFormat? format, double value) {
    return format == null ? value : double.parse(format.format(value));
  }
}
